package cj.web.marriage.controllers;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import cj.utils.Constants;
import cj.utils.ImageUtils;
import cj.utils.ReadConfigProperties;
import cj.utils.ReturnUUID;
import cj.web.admin.constants.MemuEnum;
import cj.web.login.controllers.BaseController;
import cj.web.marriage.dao.VoteDAO;
import cj.web.marriage.dao.VoteItemDAO;
import cj.web.marriage.domain.Vote;
import cj.web.marriage.domain.VoteItem;

@Path("/voteItem")
public class VoteItemController extends BaseController{

	private static Logger logger = Logger.getLogger(VoteItemController.class.getName());
	
	@Autowired
	private VoteDAO voteDao;
	@Autowired
	private VoteItemDAO voteItemDao;
	
	@Get("show")
	public String show(Model model,VoteItem voteItem) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.voteItem);
        List<Vote> objlis = voteDao.queryAll();
        model.add("objlis",objlis);
		return "/views/marriage/voteItemList.vm";
	}

	@Post("getList")
	public String queryList(VoteItem voteItem) {
		JSONObject jo = new JSONObject();
		List<VoteItem> objs = voteItemDao.query(voteItem);
		int size = voteItemDao.querySize(voteItem);
		jo.put("data", objs);
		jo.put("draw", voteItem.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model)
	{
		//MemuEnum
        model.add("MemuEnum",MemuEnum.voteItem);
        List<Vote> objlis = voteDao.queryAll();
        model.add("objlis",objlis);
		return "/views/marriage/voteItemDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,VoteItem voteItem,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			voteItem.setCreateUser(returnSessionObj(request).getAccount());
			voteItem.setUpdateUser(returnSessionObj(request).getAccount());
			voteItemDao.create(voteItem);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		VoteItem obj = voteItemDao.queryById(id);
		if(obj!=null){
			model.add("obj",obj);
		}
		//MemuEnum
        model.add("MemuEnum",MemuEnum.voteItem);
        List<Vote> objlis = voteDao.queryAll();
        model.add("objlis",objlis);
		return "/views/marriage/voteItemEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,VoteItem voteItem,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			voteItem.setUpdateUser(returnSessionObj(request).getAccount());
			voteItemDao.update(voteItem);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id)
	{
		voteItemDao.deleteById(id);
		return "r:show";
	}
	
	@Post("doUpload")
    public String doUpload(@Param("uploadimage") MultipartFile file,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		// 判断文件是否为空  
        if (file != null && !file.isEmpty()) {  
            try {  
                // 文件保存路径  
            	String path = ReadConfigProperties.getValue("uploadFilePath_Marriage");
            	//判断文件是否存在，不存在创建文件
            	Map rep = ReturnUUID.getFolder(path);
            	path = rep.get("allpath").toString();//所在的全路径
            	//文件名
                String fileName =  ReturnUUID.getName(file.getOriginalFilename()); 
                // 转存文件  
                file.transferTo(new File(path+"/"+fileName));  
                
                jo.put("filePath", rep.get("filepath")+"/"+fileName);
                
                ImageUtils.scale2(path+"/"+fileName, path+"/s_"+fileName, 120, 120, true);//缩率图
                
            } catch (Exception e) {  
                e.printStackTrace();  
                jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
            }  
        } 
		
		return "@"+jo.toString();
    }
	
	@Get("updateBanById")
	public String updateBanById(Model model,@Param("id") int id)
	{
		voteItemDao.updateBanById(id);
		return "r:show";
	}
	
	@Get("updateChannelBanById")
	public String updateChannelBanById(Model model,@Param("id") int id)
	{
		voteItemDao.updateChannelBanById(id);
		return "r:show";
	}
	
	@Get("addCountPage")
	public String addCountPage(Model model,@Param("id") int id){
		//MemuEnum
		VoteItem obj = voteItemDao.queryById(id);
        model.add("obj",obj);
        model.add("MemuEnum",MemuEnum.voteItem);
		return "/views/marriage/voteItemCount.vm";
	}
	
	@Post("updateCountById")
	public String updateCountById(Model model,@Param("id") int id,@Param("count") int count){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			voteItemDao.updateCountById(id,count);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
		
	}
}